package com.sskj.qiangjin.ui.activity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sskj.common.base.App;
import com.sskj.common.util.CheckUtil;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.qiangjin.R;
import com.sskj.qiangjin.bean.BackUserBean;
import com.sskj.qiangjin.component.DaggerUserDataComponent;
import com.sskj.qiangjin.presenter.LoginActivityPresenter;
import com.sskj.qiangjin.util.CodeUtils;

import javax.inject.Inject;

import butterknife.BindView;

@Route(path = RConfig.APP_LOGIN)//登陆
public class LoginActivity extends BaseActivity<LoginActivityPresenter> {
    @BindView(R.id.et_login_tel)
    EditText etLoginTel;
    @BindView(R.id.et_login_pwd)
    EditText etLoginPwd;
    @BindView(R.id.et_login_verification_code)
    EditText etLoginVerificationCode;
    @BindView(R.id.tv_forget_pwd)
    TextView tvForgetPwd;
    @BindView(R.id.tv_login)
    TextView tvLogin;
    @BindView(R.id.iv_verification_code)
    ImageView ivVerificationCode;
    @BindView(R.id.tv_login_register)
    TextView tvLoginRegister;
    @Inject
    UserViewModel userViewModel;
    private Bitmap bitmap;
    private CodeUtils codeUtils;

    @Override
    protected int getLayoutResId() {
        return R.layout.app_activity_login;
    }

    @Override
    public LoginActivityPresenter getPresenter() {
        return new LoginActivityPresenter();
    }

    @Override
    protected void initView() {
        setTitle(App.INSTANCE.getString(R.string.app_activity_login));
        DaggerUserDataComponent.create().inject(this);

        codeUtils = CodeUtils.getInstance();

        bitmap = codeUtils.createBitmap();
        ivVerificationCode.setImageBitmap(bitmap);
        ClickUtil.click(ivVerificationCode, () -> {
            bitmap = codeUtils.createBitmap();
            ivVerificationCode.setImageBitmap(bitmap);
        });
        ClickUtil.click(tvForgetPwd, () -> {
            ARouter.getInstance().build(RConfig.APP_FORGET_PWD).navigation();
        });
        ClickUtil.click(tvLogin, () -> {
            if (!CheckUtil.isMobile(etLoginTel.getText().toString())) {
                ToastUtil.showShort("请输入正确的手机号");
                return;
            }
            if (TextUtils.isEmpty(etLoginPwd.getText().toString())) {
                ToastUtil.showShort("请输入密码");
                return;
            }
            if (!CheckUtil.isPayPwd(etLoginPwd.getText().toString())) {
                ToastUtil.showShort("密码格式错误");
                return;
            }
            String verificationCode = etLoginVerificationCode.getText().toString();
            if (TextUtils.isEmpty(verificationCode)) {
                ToastUtil.showShort("请输入验证码");
                return;
            }
            String code = codeUtils.getCode();
            if (!code.equalsIgnoreCase(verificationCode)) {
                ToastUtil.showShort("验证码错误");
                bitmap = codeUtils.createBitmap();
                ivVerificationCode.setImageBitmap(bitmap);
                return;
            }
            mPresenter.login(etLoginTel.getText().toString(), etLoginPwd.getText().toString());
        });
        ClickUtil.click(tvLoginRegister, () -> {
            ARouter.getInstance().build(RConfig.APP_REGISTER).navigation();
        });
    }

    public void loginSuccess(BackUserBean backUserBean) {
        ARouter.getInstance().build(RConfig.APP_MAIN).navigation();
        UserData userData = new UserData();
        userData.setId(0);
        userData.setAccount(backUserBean.getAccount());
        userData.setToken(backUserBean.getToken());
        userData.setTel(etLoginTel.getText().toString());
        userViewModel.insert(userData);
        finish();
    }

}
